package cn.agiledata.bank.transaction.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.agiledata.bank.common.dao.HqlPageListTemplate;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.hibernateBean.BankInfo;
import cn.agiledata.bank.transaction.logic.ReturnQueryBean;

public class ReturnTransQueryPageList extends HqlPageListTemplate {
    private ReturnQueryBean queryBean;
    
    private List listIds;
    
    public ReturnTransQueryPageList(ReturnQueryBean bean) {
    	queryBean = bean;
	}
    
    public ReturnTransQueryPageList(Long[] ids,ReturnQueryBean bean) {
    	if(ids != null && ids.length > 0) {
    		listIds = new ArrayList();
    		for (int i = 0; i < ids.length; i++) {
				listIds.add(ids[i]);
			}
    	}
    	queryBean = bean;
    }

	/*
     * 可进行退款的交易类型
     */
    private static List RETURN_ACTION_TYPE;
    
    static {
    	// 暂不提供充值类型退款
    	RETURN_ACTION_TYPE = new ArrayList();
    	RETURN_ACTION_TYPE.add(new Long(Constant.ACTION_TYPE_F));		// B2C网上银行消费
    	RETURN_ACTION_TYPE.add(new Long(Constant.ACTION_TYPE_Q));		// 卡付款
    }
    
    public List getReturnActionType() {
		return RETURN_ACTION_TYPE;
	}

	/**
     * 取得支持退款的银行ID
     * 当前所有银行都支持，所以取得所有银行
     * 
     * @return
     */
    private List getSupportBank() {
    	String strOql = "select bi from BankInfo bi where bi.bankCode <> ?";
    	
    	// 农行不支持退款
    	List listParam = new ArrayList();
    	listParam.add(Constant.BANK_CODE_ABC);
    	List listResult = getHibernateTemplate().find(strOql,
				listParam.toArray());
    	
    	List listReturn = null;
    	if(listResult != null && listResult.size() > 0) {
    		listReturn = new ArrayList();
    		for (Iterator iter = listResult.iterator(); iter.hasNext();) {
				BankInfo bi = (BankInfo) iter.next();
				listReturn.add(bi.getId());
			}
    	}
    	
    	return listReturn;
    }

    public String getHql() {
        String hql = "select t from Transaction t where t.credit.account=:account";
        if (this.queryBean.getSubAccountId() != null) {
            hql += " and t.credit.id=:subAccountId ";
        }
            
        if (this.queryBean.getBeginDate() != null) {
            hql += " and t.beginDate>=:beginDate ";
        }
        if (this.queryBean.getEndDate() != null) {
            hql += " and t.beginDate<=:endDate ";
        }
        if (this.queryBean.getPeerAccount() != null) {
            hql += " and t.credit.account=:peerAccount ";
        }
        if (this.queryBean.getTransId() != null
                && this.queryBean.getTransId().length() > 0) {
            hql += " and t.transId like :transId ";
        }
        if (this.queryBean.getOrderId() != null
                && this.queryBean.getOrderId().length() > 0) {
            hql += " and upper(t.orderId) like :orderId ";
        }
        if (this.queryBean.getMinAmount() != null) {
            hql += " and t.amount>=:minAmount ";
        }
        if (this.queryBean.getMaxAmount() != null) {
            hql += " and t.amount<=:maxAmount ";
        }
        if (this.queryBean.getActionType() != null) {
            hql += " and t.actionType.id = :actionType ";
        }
        if (listIds != null) {
			hql += " and t.id in (:ids) ";
		}
        
        
        hql += " and t.transStatus = :transStatus ";
        //hql += " and t.actionType.id in (:actionTypeList) ";
        hql += " and (t.actionType.id = :actionTypeQ or t.actionType.id = :actionTypeF)";
        hql += " and not exists(select 1 from EsPrePayTrans pTrans where pTrans.transStatus in (:pTransStatus) " +
        		" and pTrans.relating = t.id )";
        // 银行限制
        hql += " and t.bankSeq in (:bankIds)";
        hql += " order by t.beginDate desc";
        return hql;
    }

    public String getPageRecord() {
        return String.valueOf(Constant.PAGESIZE);
    }

    public Map getParams() {
        Map params = new HashMap();
        params.put("account", this.queryBean.getAccount());
        if (this.queryBean.getSubAccountId() != null) {
            params.put("subAccountId", this.queryBean.getSubAccountId());
        }
            
        if (this.queryBean.getBeginDate() != null) {
            params.put("beginDate", DateUtil.earliestOfDate(this.queryBean.getBeginDate()));
        }
        Date endDate = this.queryBean.getEndDate();
        if (endDate != null) {
            endDate = DateUtil.latestOfDate(endDate);
            params.put("endDate", endDate);
        }
        if (this.queryBean.getPeerAccount() != null) {
            params.put("peerAccount", this.queryBean.getPeerAccount());
        }
        if (this.queryBean.getTransId() != null
                && this.queryBean.getTransId().length() > 0) {
            params.put("transId", "%" + this.queryBean.getTransId() + "%");
        }
        if (this.queryBean.getOrderId() != null
                && this.queryBean.getOrderId().length() > 0) {
            params.put("orderId", "%" + this.queryBean.getOrderId().toUpperCase() + "%");
        }
        if (this.queryBean.getMinAmount() != null) {
            params.put("minAmount", this.queryBean.getMinAmount());
        }
        if (this.queryBean.getMaxAmount() != null) {
            params.put("maxAmount", this.queryBean.getMaxAmount());
        }
        if (this.queryBean.getActionType() != null) {
            params.put("actionType", this.queryBean.getActionType());
        }
        if (listIds != null) {
			params.put("ids", listIds);
		}
        
        // 待企业审批的退款申请状态
        List listPTransStatus = new ArrayList();
        listPTransStatus.add(Constant.ES_TRANS_STATUS_TOPUBMIT);	// 待提交
        listPTransStatus.add(Constant.ES_TRANS_STATUS_UNAUDIT);		// 待审核
        listPTransStatus.add(Constant.ES_TRANS_STATUS_FINISH);		// 完成
        listPTransStatus.add(Constant.ES_TRANS_STATUS_UNPAY);		// 待支付
        listPTransStatus.add(Constant.ES_TRANS_STATUS_REJECTED);		// 审核拒绝
        
        params.put("pTransStatus", listPTransStatus);
        params.put("transStatus", Constant.TRANS_STATUS_TRANSACTIONSUCCESS);
        //params.put("actionTypeList", RETURN_ACTION_TYPE);
        params.put("actionTypeF", new Long(Constant.ACTION_TYPE_F));
        params.put("actionTypeQ", new Long(Constant.ACTION_TYPE_Q));
        params.put("bankIds", getSupportBank());
        return params;
    }

    public String getStartPage() {
        String startPage = this.queryBean.getStartPage();
        if (startPage == null || startPage.length() == 0) {
            startPage = "1";
        }
        return startPage;
    }

}
